package com.ruoyi.energy.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.energy.service.INyCheckMeterService;
import com.ruoyi.omp.api.domain.NyCheckMeter;
import com.ruoyi.omp.api.domain.vo.NyCheckMeterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * 抄表记录Controller
 *
 * @author cjie
 * @date 2021-07-29
 */
@Api(tags = " 抄表记录")
@RestController
@RequestMapping("/nyCheckMeter")
public class NyCheckMeterController extends BaseController {
    @Autowired
    private INyCheckMeterService nyCheckMeterService;

//    @Autowired
//    private INyCheckSumService nyCheckSumService;
    /**
     * 查询抄表记录列表
     * 小程序，历史度数，仪表id
     */
    @ApiOperation(value = "查询抄表记录", notes = "查询抄表记录")
    @PreAuthorize(hasPermi = "energy:nyCheckMeter:list")
    @GetMapping("/list")
    public TableDataInfo list(NyCheckMeter nyCheckMeter) {
        startPage();
        List<NyCheckMeter> list = nyCheckMeterService.selectNyCheckMeterList(nyCheckMeter);
        return getDataTable(list);
    }

    /*
     * @Params feeCycle，meterType,parkNum，
     * 远程接口 查询总表抄表记录
     *
     */
    @ApiOperation(value = "R-查询总表当月分摊", notes = "R-查询总表当月分摊")
    @PostMapping(value = "/listCheckMonthShare")
    R<List<NyCheckMeterVO>> listCheckMonthShare(@RequestBody NyCheckMeter nyCheckMeter){
        List<NyCheckMeterVO> list = nyCheckMeterService.selectCheckMeterList(nyCheckMeter);
        return R.ok(list);
    }

    /*
     * @Params feeCycle，meterType,parkNum，
     * 远程接口 查询总表抄表记录
     *
     */
    @ApiOperation(value = "R-查询总表及私表记录", notes = "R-查询总表及私表记录")
    @PostMapping(value = "/listCheck")
    R<List<NyCheckMeterVO>> listCheckMeter(@RequestBody NyCheckMeter nyCheckMeter){
        List<NyCheckMeterVO> list = nyCheckMeterService.selectCheckMeterSumAndPrivateList(nyCheckMeter);
        return R.ok(list);
    }
    /**
     * 导出抄表记录列表,
     */
    @ApiOperation(value = "导出抄表记录", notes = "导出抄表记录")
    @PreAuthorize(hasPermi = "energy:nyCheckMeter:export")
    @Log(title = "抄表记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, NyCheckMeter nyCheckMeter) throws IOException {
        List<NyCheckMeter> list = nyCheckMeterService.selectNyCheckMeterList(nyCheckMeter);
        ExcelUtil<NyCheckMeter> util = new ExcelUtil<NyCheckMeter>(NyCheckMeter.class);
        util.exportExcel(response, list, "抄表记录数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @ApiOperation(value = "导入抄表记录", notes = "导入抄表记录")
    @PreAuthorize(hasPermi = "energy:nyCheckMeter:edit")
    @Log(title = "导入抄表记录", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {

        ExcelUtil<NyCheckMeter> util = new ExcelUtil<NyCheckMeter>(NyCheckMeter.class);
        List<NyCheckMeter> checkMeters = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = nyCheckMeterService.importNyCheckMeter(checkMeters);
        return AjaxResult.success(message);

    }

    /**
     * 导入模板,
     */
    @ApiOperation(value = "下载导入模板", notes = "下载导入模板")
    @PreAuthorize(hasPermi = "energy:nyCheckMeter:export")
    @Log(title = "下载导入模板", businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response, NyCheckMeter nyCheckMeter) throws IOException {
        List<NyCheckMeter> list = nyCheckMeterService.genImportTemplate(nyCheckMeter);
        ExcelUtil<NyCheckMeter> util = new ExcelUtil<NyCheckMeter>(NyCheckMeter.class);
        util.exportExcel(response, list, "导入模板");
    }

    /**
     * 获取分摊池和月分摊量
     */
    @ApiOperation(value = "获取分摊池和月分摊量", notes = "获取分摊池和月分摊量")
    @GetMapping(value = "/getMonthShare")
    public AjaxResult getMonthShare(@PathVariable("id") String id) {
        return AjaxResult.success(nyCheckMeterService.selectNyCheckMeterById(id));
    }

    /**
     * 获取抄表记录详细信息
     */
    @ApiOperation(value = "获取抄表记录详细信息", notes = "获取抄表记录详细信息")
    @PreAuthorize(hasPermi = "energy:nyCheckMeter:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(nyCheckMeterService.selectNyCheckMeterById(id));
    }

    /**
     * 获取抄表记录详细信息(内部调用)
     */
    @GetMapping(value = "/info/{recordId}")
    public R<NyCheckMeter> getRoom(@PathVariable("recordId") String recordId) {
        return R.ok(nyCheckMeterService.selectNyCheckMeterById(recordId));
    }



//    @ApiOperation(value = "微信接口获取抄表记录详细信息", notes = "微信接口获取抄表记录详细信息")
//    @PreAuthorize(hasPermi = "energy:nyCheckMeter:query")
//    @GetMapping(value = "/wx/{id}")
//    public AjaxResult getWxInfo(@PathVariable("id") String id) {
//        return AjaxResult.success(nyCheckMeterService.selectWXNyCheckMeterById(id));
//    }

    /**
     * 新增抄表记录
     */
    @ApiOperation(value = "新增抄表记录", notes = "新增抄表记录")
    @PreAuthorize(hasPermi = "energy:nyCheckMeter:add")
    @Log(title = "抄表记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NyCheckMeter nyCheckMeter) {
        return toAjax(nyCheckMeterService.insertNyCheckMeter(nyCheckMeter));
    }

    /**
     * 修改抄表记录
     */
    @ApiOperation(value = "修改抄表记录", notes = "修改抄表记录")
    @PreAuthorize(hasPermi = "energy:nyCheckMeter:edit")
    @Log(title = "抄表记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NyCheckMeter nyCheckMeter) {
        return toAjax(nyCheckMeterService.updateNyCheckMeter(nyCheckMeter));
    }

    /**
     * 修改抄表记录(内部调用)
     * @return
     */
    @Log(title = "修改抄表记录", businessType = BusinessType.UPDATE)
    @PostMapping("/updateCheckMeter")
    public R updateCheckMeter(@RequestBody NyCheckMeter nyCheckMeter) {
        return toR(nyCheckMeterService.updateNyCheckMeter(nyCheckMeter));
    }

    /**
     * 删除抄表记录
     */
    @ApiOperation(value = "删除抄表记录", notes = "删除抄表记录")
    @ApiImplicitParam(name = "ids", value = "抄表记录ID数组", required = true, paramType = "path", allowMultiple = true, dataType = "String")
    @PreAuthorize(hasPermi = "energy:nyCheckMeter:remove")
    @Log(title = "抄表记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(nyCheckMeterService.deleteNyCheckMeterByIds(ids));
    }

    /**
     * 计算私表汇总
     */
    @ApiOperation(value="私表汇总量", notes="私表汇总量")
    @Log(title = "私表汇总量")
    @PostMapping(value = "/compute/private")
    R<BigDecimal> computePrivateSummery(@RequestBody NyCheckMeter nyCheckMeter){
        BigDecimal result = nyCheckMeterService.computePrivateSummary(nyCheckMeter);
        return R.ok(result);
    }

    /**
     * 计算私表汇总
     */
    @ApiOperation(value="计算总表汇总量", notes="总表汇总量")
    @Log(title = "计算总表汇总量")
    @GetMapping(value="/compute/{id}")
    public AjaxResult compute(@PathVariable("id") String id ) {
        return AjaxResult.success(nyCheckMeterService.computeNyMeterSummary(id));
    }

//    @ApiOperation(value = "查询抄表记录", notes = "查询抄表记录")
//    @GetMapping("/checkSum/list")
//    public AjaxResult compute1(NyCheckMeter nyCheckMeter) {
//        return AjaxResult.success(nyCheckMeterService.computeNyMeterSummary(nyCheckMeter));
//    }

    @ApiOperation(value = "查询抄表记录", notes = "查询抄表记录")
    @GetMapping("/list2")
    public AjaxResult compute2(NyCheckMeter nyCheckMeter) {
        return toAjax(nyCheckMeterService.computeNyMeterSummaryAll(nyCheckMeter));
    }

    /**
     *  参数：园区、费用周期，选填：meterNum
     * @param nyCheckMeter
     * @return
     */
    @ApiOperation(value = "计算抄表记录", notes = "计算抄表记录")
    @GetMapping("/result")
    public TableDataInfo computePlus(NyCheckMeter nyCheckMeter) {
        //return toAjax(nyCheckMeterService.computeNyMeterSummaryAll(nyCheckMeter));
        startPage();
        AjaxResult test = nyCheckMeterService.computeNyMeterSummaryAllPlus(nyCheckMeter);
        TableDataInfo result = getDataTable((List)test.get(AjaxResult.DATA_TAG));
        result.setMsg((String)test.get(AjaxResult.MSG_TAG));
        return result;
    }

    /**
     * 根据总表抄表记录查询抄表记录树,测试；
     * @param nyCheckMeter
     * @return
     */
    @ApiOperation(value = "总表抄表记录树", notes = "总表抄表记录树")
    @GetMapping("/result1")
    public AjaxResult computePlusTest(NyCheckMeter nyCheckMeter) {
        //return toAjax(nyCheckMeterService.computeNyMeterSummaryAll(nyCheckMeter));
        System.out.println("computePlus接收数据");
        if(StringUtils.isBlank(nyCheckMeter.getMeterId()) || StringUtils.isBlank(nyCheckMeter.getFeeCycle().toString())){
            return AjaxResult.error("参数错误");
        }
        NyCheckMeterVO test = nyCheckMeterService.selectNyMeterCheckVO(nyCheckMeter);
        return AjaxResult.success(test);
    }

    @ApiOperation(value="查询园区是否汇总", notes="查询园区是否汇总")
    @GetMapping("/isCompleted")
    public R<Boolean> getIsCompleted(NyCheckMeter nyCheckMeter) {

        return R.ok(nyCheckMeterService.getIsCompleted(nyCheckMeter));
    }

    /**
     *
     * @param meterType,parkNum, 废弃
     * @return
     */
    @ApiOperation(value="BP-水电月度趋势", notes="BP-水电月度趋势")
    @GetMapping("/energyTrend")
    public AjaxResult energyTrend(NyCheckMeter nyCheckMeter) {

        return AjaxResult.success(nyCheckMeterService.energyTrend(nyCheckMeter));
    }

    /**
     *
     * @param parkNum,Feecycle,meterType，废弃
     * @return group by 企业
     */
    @ApiOperation(value="BP-企业能耗记录", notes="BP-企业能耗记录")
    @GetMapping("/energyRecord")
    public AjaxResult energyStruct(NyCheckMeter nyCheckMeter) {

        return AjaxResult.success(nyCheckMeterService.energyRecord(nyCheckMeter));
    }

}
